package com.bolt.convention.exception;


import com.bolt.common.utils.StrUtil;
import com.bolt.convention.data.code.CommonCode;
import com.bolt.convention.data.code.ServiceCode;

/**
 * Created by Administrator on 2018/9/26.
 */
public class ServiceException extends RuntimeException {


    protected String code;

    public ServiceException() {
        super();
    }

    public ServiceException(String message) {
        super(message);
        this.code = CommonCode.UNKNOWN_ERROR.code();
    }

    public ServiceException(String code, String message) {
        super(message);
        this.code = code;
    }

    public ServiceException(String message, Throwable cause) {
        super(message, cause);
        this.code = CommonCode.UNKNOWN_ERROR.code();
    }

    public ServiceException(Throwable cause) {
        super(CommonCode.UNKNOWN_ERROR.message(), cause);
        this.code = CommonCode.UNKNOWN_ERROR.code();
    }

    public ServiceException(String code, String message, Throwable cause) {
        super(message, cause);
        this.code = code;
    }

    public ServiceException(ServiceCode respCode) {
        super(respCode.message());
        this.code = respCode.code();
    }

    public ServiceException(ServiceCode respCode, Throwable cause) {
        super(respCode.message(), cause);
        this.code = respCode.code();
    }

    public ServiceException(ServiceCode respCode, String messageTemplate, Object... params) {
        super(StrUtil.format(messageTemplate, params));
        this.code = respCode.code();
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    @Override
    public String toString() {
        return "ServiceException{" +
                "code='" + code + '\'' +
                "message='" + getMessage() + '\'' +
                '}';
    }
}
